package com.ww.learn.oop.sam

fun interface DiscountStrategy {
    fun calculateDiscount(amount: Double): Double
}

class ShoppingCart(private var discountStrategy: DiscountStrategy = DiscountStrategy { it }) {
    fun setDisountStrategy(strategy: DiscountStrategy) {
        this.discountStrategy = strategy
    }

    fun calculateTotal(amount: Double): Double {
        return discountStrategy.calculateDiscount(amount)
    }
}

// 打印结果
//9折后：90.0
//满减后：250.0
//固定折扣后：90.0
fun main() {
    val cart = ShoppingCart()

    // 9折策略
    cart.setDisountStrategy { amount -> amount * 0.9 }
    println("9折后：${cart.calculateTotal(100.0)}")

    // 满减策略
    cart.setDisountStrategy { amount ->
        if (amount > 200) amount - 50.0 else amount
    }
    println("满减后：${cart.calculateTotal(300.0)}")

    // 固定折扣
    cart.setDisountStrategy { amount -> amount - 10 }
    println("固定折扣后：${cart.calculateTotal(100.0)}")
}